﻿<Window
	xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
	xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
	xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:v="clr-namespace:YouDown.Views"
	mc:Ignorable="d"
	x:Class="YouDown.Views.MainView"
	x:Name="Window"
	Title="YouDown"
	MinWidth="600" MinHeight="500" Width="600" Height="500" Icon="..\Resources\YouDown.png" DataContext="{StaticResource MainVM}"
	Background="{DynamicResource WindowBackgroundGradient}" FontFamily="/YouDown;Component/Fonts/#Segoe UI">
	<Window.Resources>
        <LinearGradientBrush x:Key="WindowBackgroundGradient" EndPoint="0,1" StartPoint="1,0">
			<GradientStop Color="#FFC7E5F0"/>
			<GradientStop Color="White" Offset="1"/>
		</LinearGradientBrush>
        
        <SolidColorBrush x:Key="GrayBorder" Color="#FF999999"/>

        <Style TargetType="{x:Type Button}">
            <Setter Property="IsEnabled" Value="{Binding ControlsEnabled}" />
        </Style>
        <Style TargetType="{x:Type ComboBox}">
            <Setter Property="IsEnabled" Value="{Binding ControlsEnabled}" />
        </Style>

        <Style TargetType="{x:Type TextBox}">
            <Setter Property="IsEnabled" Value="{Binding ControlsEnabled}" />
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate />
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}"/>
                    <Setter Property="Background" Value="Pink" />
                </Trigger>
            </Style.Triggers>
        </Style>

        <v:VisibilityConverter x:Key="VisibilityConverter" />
        <v:PlaceholderVisibilityConverter x:Key="PlaceholderVisibilityConverter" />
    </Window.Resources>
    
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="40" />
            <RowDefinition />
            <RowDefinition Height="30" />
            <RowDefinition Height="100" />
        </Grid.RowDefinitions>

        <Label Content="{Binding TitleText, FallbackValue=Title}" FontSize="16" Margin="10,5,0,0" />

        <ListBox x:Name="VidsView" Grid.Row="1" Margin="10,0" BorderBrush="{DynamicResource GrayBorder}" ScrollViewer.HorizontalScrollBarVisibility="Hidden"
                 ItemsSource="{Binding Videos}" SelectedItem="{Binding SelectedVideo}" SelectionChanged="VidsView_SelectionChanged">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <StackPanel Orientation="Horizontal">
                            <Image x:Name="StateIcon" ToolTip="{Binding Error}" Height="20" Width="20" />
                            <Label x:Name="TitleLabel" Content="{Binding Title}" FontWeight="Bold" Visibility="Collapsed" />
                            <Label Content="{Binding Address}" />
                        </StackPanel>
                    </Grid>
                    
                    <DataTemplate.Triggers>
                        <DataTrigger Binding="{Binding HasTitle}" Value="True">
                            <Setter TargetName="TitleLabel" Property="Visibility" Value="Visible" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding State}" Value="Error">
                            <Setter TargetName="StateIcon" Property="Source" Value="..\Resources\Error.png" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding State}" Value="Processing">
                            <Setter TargetName="StateIcon" Property="Source" Value="..\Resources\Download.png" />
                        </DataTrigger>
                        <DataTrigger Binding="{Binding State}" Value="Done">
                            <Setter TargetName="StateIcon" Property="Source" Value="..\Resources\Yes.png" />
                        </DataTrigger>
                    </DataTemplate.Triggers>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
        
        <Grid Grid.Row="2" Margin="10,0,5,0">
            <TextBox Height="25" Text="{Binding AddressText, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}" Margin="0,0,225,0" />
            <Label Height="25" VerticalAlignment="Center" Focusable="False" IsHitTestVisible="False" Opacity="0.5" FontStyle="Italic" 
                   Visibility="{Binding AddressText, Converter={StaticResource PlaceholderVisibilityConverter}}">Video address</Label>
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Right">
                <StackPanel.Resources>
                    <Style TargetType="{x:Type Button}">
                        <Setter Property="Margin" Value="0,0,5,0" />
                        <Setter Property="Width" Value="50" />
                        <Setter Property="Height" Value="25" />
                        <Setter Property="IsEnabled" Value="{Binding ControlsEnabled}" />
                    </Style>
                </StackPanel.Resources>
                <Button Command="{Binding AddCommand}">Add</Button>
                <Button Command="{Binding RemoveCommand}" IsEnabled="{Binding RemoveButtonEnabled}">Remove</Button>
                <Button Command="{Binding ClearCommand}" IsEnabled="{Binding ClearButtonEnabled}">Clear</Button>
                <Button Command="{Binding ImportCommand}">Import</Button>
            </StackPanel>
        </Grid>

        <Grid Grid.Row="3" Margin="10,10,100,10" Visibility="{Binding Working, Converter={StaticResource VisibilityConverter}}">
            <Grid.RowDefinitions>
                <RowDefinition />
                <RowDefinition />
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="110" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            
            <Label VerticalAlignment="Center">Processing videos</Label>
            <ProgressBar Grid.Column="1" Value="{Binding ProcessProgress}" Height="20" />
            <Label  VerticalAlignment="Center" Grid.Row="1">Downloading</Label>
            <ProgressBar Grid.Row="1" Grid.Column="1" Value="{Binding DownloadProgress}" Height="20" />
        </Grid>
        
        <GroupBox Header="Options" Grid.Row="3" Margin="10,0,100,10" BorderBrush="{DynamicResource GrayBorder}" Background="White" 
                  Visibility="{Binding Working, Converter={StaticResource VisibilityConverter}, ConverterParameter=true}">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="0.5*"/>
                    <RowDefinition Height="0.5*"/>
                </Grid.RowDefinitions>

                <Label Content="Maximum Quality:"/>
                <ComboBox x:Name="QualityBox" Margin="110,0,0,0" VerticalAlignment="Center" 
                          ItemsSource="{Binding QualityLevels}" DisplayMemberPath="Description" SelectedIndex="2" SelectedItem="{Binding SelectedQuality}" />

                <Label Content="Save to:" Grid.Row="1"/>
                <TextBox x:Name="PathBox" Grid.Row="1" Margin="110,0,35,0" VerticalAlignment="Center" IsReadOnly="True" Text="{Binding TargetPath}" ToolTip="{Binding TargetPath}"/>
                <Button Content="..."  Grid.Row="1" VerticalAlignment="Center" Width="30" HorizontalAlignment="Right" Command="{Binding PathCommand}" />
            </Grid>
        </GroupBox>
        
        <Button x:Name="GoButton" Width="70" Content="Go" Grid.Row="3" Height="70" HorizontalAlignment="Right" Margin="0,0,10,0" Command="{Binding GoCommand}" IsEnabled="{Binding GoButtonEnabled}" />
        <Button x:Name="StopButton" Width="70" Content="Stop" Grid.Row="3" Height="70" HorizontalAlignment="Right" Margin="0,0,10,0" Command="{Binding StopCommand}" IsEnabled="{Binding StopButtonEnabled}"
                Visibility="{Binding Working, Converter={StaticResource VisibilityConverter}}"/>
    </Grid>
</Window>